package org.nci.platform.workflow.controller;


import gh.priv.general.common.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.nci.platform.workflow.dto.FlowTaskDto;
import org.nci.platform.workflow.service.IFlowInstanceService;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>工作流流程实例管理<p>
 *
 * @author Tony
 * @date 2021-04-03
 */
@Slf4j
@Tag(name = "工作流流程实例管理")
@RestController
@RequestMapping("/flowable/instance")
public class FlowInstanceController {

    @Resource
    private IFlowInstanceService flowInstanceService;

    @Operation(summary = "根据流程定义id启动流程实例")
    @PostMapping("/startBy/{procDefId}/{currUserId}")
    public Result<Boolean> startById(@Parameter(description = "流程定义id") @PathVariable(value = "procDefId") String procDefId,
                            @Parameter(description = "用户id") @PathVariable(value = "currUserId") String currUserId,
                                @Parameter(description = "变量集合,json对象") @RequestBody Map<String, Object> variables) {
        flowInstanceService.startProcessInstanceById(procDefId,currUserId, variables);
        return Result.success();

    }

    @Operation(summary = "激活或挂起流程实例")
    @PostMapping(value = "/updateState")
    public Result<Boolean> updateState(@Parameter(description = "1:激活,2:挂起", required = true) @RequestParam Integer state,
                                  @Parameter(description = "流程实例ID", required = true) @RequestParam String instanceId) {
        flowInstanceService.updateState(state,instanceId);
        return Result.success();
    }

    @Operation(summary = "结束流程实例")
    @PostMapping(value = "/stopProcessInstance")
    public Result<Boolean> stopProcessInstance(@RequestBody FlowTaskDto flowTaskDto) {
        flowInstanceService.stopProcessInstance(flowTaskDto);
        return Result.success();
    }

    @Operation(summary = "删除流程实例")
    
    @DeleteMapping(value = "/delete/{instanceIds}")
    public Result<Boolean> delete(@Parameter(description = "流程实例ID", required = true) @PathVariable String[] instanceIds,
                             @Parameter(description = "删除原因") @RequestParam(required = false) String deleteReason) {
        for (String instanceId : instanceIds) {
            flowInstanceService.delete(instanceId,deleteReason);
        }
        return Result.success();
    }
}
